
package com.netease.xone.widget;

import java.util.ArrayList;
import java.util.List;

import protocol.meta.Subject;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.netease.framework.widget.LinkedListAdapter;
import com.netease.xone.R;

public class SubjectBannerView extends RelativeLayout implements OnItemSelectedListener {

    // Context
    private Context mContext;

    // Views
    private ScrollGallery mScrollGallery;
    private TextView mBannerTitle;
    private BannerIndicator mBannerIndicator;

    // Data
    private List<Subject> mList;
    private int mPosition = 0;
    private GalleryAdapter mGalleryAdapter;
    private OnItemClickListener mOnItemClickListener;
    private boolean isAutoScroll;

    public SubjectBannerView(Context context) {
        this(context, null, 0);
    }

    public SubjectBannerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SubjectBannerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, null);
    }

    public SubjectBannerView(Context context, List<Subject> list) {
        super(context, null, 0);
        init(context, list);
    }

    private void init(Context context, List<Subject> list) {
        mContext = context;
        mList = new ArrayList<Subject>();
        if (list != null)
            mList.addAll(list);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mScrollGallery = (ScrollGallery)findViewById(R.id.banner_gallery);
        mBannerTitle = (TextView)findViewById(R.id.banner_title);
        mBannerIndicator = (BannerIndicator)findViewById(R.id.banner_indicator);
        initViews();
    }

    private void initViews() {
        mGalleryAdapter = new GalleryAdapter(mContext);
        mGalleryAdapter.addHead(mList);
        mScrollGallery.setOnItemClickListener(mOnItemClickListener);
        mScrollGallery.setAdapter(mGalleryAdapter);
        mScrollGallery.setOnItemSelectedListener(this);
        mScrollGallery.setAutoScroll(isAutoScroll);
        mBannerIndicator.setCount(mGalleryAdapter.getCount());
        mPosition = 0;
    }

    public int getCount() {
        return mList.size();
    }

    public int getPosition() {
        return mPosition;
    }

    public void setPosition(int position) {
        mScrollGallery.setSelection(position);
    }

    public Subject getItem(int position) {
        return mList.get(position);
    }

    public void setData(List<Subject> list) {
        if (mList.size() != 0)
            mList.clear();
        mList.addAll(list);
        if (mScrollGallery != null)
            initViews();
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mOnItemClickListener = listener;
        if (mScrollGallery != null)
            mScrollGallery.setOnItemClickListener(mOnItemClickListener);
    }

    public void setTitleVisibility(boolean visibility) {
        if (visibility) {
            mBannerTitle.setVisibility(View.VISIBLE);
        } else {
            mBannerTitle.setVisibility(View.GONE);
        }
    }

    public void setIndicatorVisibility(boolean visibility) {
        View indicatorContainer = findViewById(R.id.banner_indicator_container);
        if (visibility) {
            indicatorContainer.setVisibility(View.VISIBLE);
            mBannerIndicator.setVisibility(View.VISIBLE);
        } else {
            indicatorContainer.setVisibility(View.GONE);
            mBannerIndicator.setVisibility(View.GONE);
        }
    }

    public static class GalleryAdapter extends LinkedListAdapter<Subject> {

        Context mContext;
        private int mWidth;
        private int mHeight;

        public GalleryAdapter(Context context) {
            mContext = context;
            mWidth = context.getResources().getDisplayMetrics().widthPixels;
            mHeight = context.getResources().getDimensionPixelSize(
                    R.dimen.recommdation_banner_height);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Subject subject = getItem(position);
            if (convertView == null) {
                LoadingImageView image = new LoadingImageView(mContext);
                image.setLayoutParams(new Gallery.LayoutParams(Gallery.LayoutParams.MATCH_PARENT,
                        mHeight));
                image.setServerClipSize(mWidth, 0);//宽图，根据宽度裁剪
                convertView = image;
            }
            ((LoadingImageView)convertView).setScaleType(ScaleType.CENTER_CROP);
            ((LoadingImageView)convertView).setLoadingImage(subject.bannerUrl);
            return convertView;
        }

        @Override
        public void clear() {
            super.clear();
            mContext = null;
        }
    }

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
        if (mList != null && mList.size() > 0) {
            Subject subject = mList.get(position);
            mBannerTitle.setText(subject.subjectName);
            mBannerIndicator.setCurrentItem(position);
            mPosition = position;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {

    }

    public void setAutoScroll(boolean autoScroll) {
        isAutoScroll = autoScroll;
        if (mScrollGallery != null)
            mScrollGallery.setAutoScroll(isAutoScroll);
    }

    public void clear() {
        mContext = null;
        mScrollGallery = null;
        mBannerTitle = null;
        if (null != mBannerIndicator) {
            mBannerIndicator.clear();
        }
        mBannerIndicator = null;
        if (null != mList) {
            mList.clear();
        }
        mList = null;
        if (null != mGalleryAdapter) {
            mGalleryAdapter.clear();
        }
        mGalleryAdapter = null;
        mOnItemClickListener = null;
    }
}
